(async () => {
  //导入连接mongodb模块
  const promise = require("./db/connect");
  try {
    //连接数据库
    await promise;
    //导入 express
    const express = require("express");
    const cors = require('cors');
    //导入用户的路由规则
    const userRouter = require("./routers/userRouter");
    const loginRouter = require("./routers/loginRouter");
    //导入验证用户身份的中间件
    const tokenVerifyMiddleware = require("./middlewares/tokenVerifyMiddleware");

    //导入配置
    const { PORT } = require("./config");
    //创建应用对象
    const app = express();
    app.use(cors());
    //设置静态资源文件夹
    app.use(express.static(__dirname + "/public"));
    //设置路由规则
    app.get("/", (req, res) => {
      res.send("首页");
    });
    //设置用户的路由
    app.use("/admin", tokenVerifyMiddleware, userRouter);
    app.use("/user", loginRouter);
    //监听端口启动服务
    app.listen(PORT, () => {
      console.log(`服务已经启动, waiting for connection on port ${PORT}`);
    });
  } catch (e) {
    //输出错误对象
    console.log(e);
    console.log("数据库连接失败~~~");
  }
})();
